package org.example.websockettest.websocket;

import com.alibaba.fastjson.JSON;
import org.example.websockettest.param.RequestMessage;
import org.example.websockettest.param.ResponseMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.util.HtmlUtils;
import org.springframework.stereotype.Controller;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@RestController
public class StompSocketHandler {

    @Autowired
    private SimpMessagingTemplate messagingTemplate;
    private static final Logger LOGGER = LoggerFactory.getLogger(StompSocketHandler.class);

    @MessageMapping("/chat")
    public void messageHandling(RequestMessage requestMessage) throws Exception {
        LOGGER.info(String.valueOf(requestMessage.getSender()+requestMessage.getContent()+requestMessage.getRoom()));

        // Ensure non-null values before escaping
        String sender = HtmlUtils.htmlEscape(requestMessage.getSender() != null ? requestMessage.getSender() : "");
        String room = HtmlUtils.htmlEscape(requestMessage.getRoom() != null ? requestMessage.getRoom() : "");
        String type = HtmlUtils.htmlEscape(requestMessage.getType() != null ? requestMessage.getType() : "");
        String content = HtmlUtils.htmlEscape(requestMessage.getContent() != null ? requestMessage.getContent() : "");
        String timestamp = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        if (sender.isEmpty() || room.isEmpty() || type.isEmpty() || content.isEmpty()) {
            throw new IllegalArgumentException("All fields are required");
        }

        String destination = "/topic/" + room;
        ResponseMessage response = new ResponseMessage(sender, type, content,timestamp);
        messagingTemplate.convertAndSend(destination, response);
    }

//    @MessageMapping("/chatAll")
//    public void messageHandlingAll(RequestMessage requestMessage) throws Exception {
//        LOGGER.info("chatAll");
//
//        // Ensure non-null values before escaping
//        String sender = HtmlUtils.htmlEscape(requestMessage.getSender() != null ? requestMessage.getSender() : "");
//        String type = HtmlUtils.htmlEscape(requestMessage.getType() != null ? requestMessage.getType() : "");
//        String content = HtmlUtils.htmlEscape(requestMessage.getContent() != null ? requestMessage.getContent() : "");
//
//        // Ensure non-null values before proceeding
//        if (sender.isEmpty() || type.isEmpty() || content.isEmpty()) {
//            throw new IllegalArgumentException("All fields are required");
//        }
//
//        String destination = "/all";
//        ResponseMessage response = new ResponseMessage(sender, type, content);
//        messagingTemplate.convertAndSend(destination, response);
//    }
}
